﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace Marmat.Forms.Views
{
    namespace Grid
    {
        public partial class Column
        {
            internal BaseGrid Grid { get; set; }

            /// <summary>
            /// 获取或设置是否允许编辑此列。
            /// </summary>
            /// <returns></returns>
            [Category("数据编辑")]
            [Description("获取或设置是否允许编辑此列。")]
            [Localizable(true)]
            [RefreshProperties(RefreshProperties.All)]
            [DefaultValue(true)]
            public bool AllowEdit
            {
                get => this.m_AllowEdit;
                set
                {
                    this.m_AllowEdit = value;
                    this.GridColumn.OptionsColumn.AllowEdit = value;
                    this.Grid?.NotifyPropertyChanged();
                }
            }

            private bool m_AllowEdit = true;
            private string _Caption;
            /// <summary>
            /// 获取或设置列的标题
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [Category("外观")]
            [Description("获取或设置列的标题。")]
            [Localizable(true)]
            [RefreshProperties(RefreshProperties.All)]
            public string Caption
            {
                get
                {
                    if (this._Caption is null || string.IsNullOrEmpty(this._Caption))
                        return this.Name;
                    return this._Caption;
                }
                set
                {
                    this._Caption = value;
                    this.GridColumn.Caption = this.Caption;
                }
            }

            private Type _DataType = typeof(object);
            /// <summary>
            /// 获取或设置存储在列中的类型
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [DefaultValue(typeof(object))]
            [TypeConverter(typeof(DataTypeConverter))]
            [Category("数据类型")]
            [Description("获取或设置存储在列中的类型。")]
            public virtual Type DataType
            {
                get => this._DataType;
                set => this._DataType = value;
            }

            /// <summary>
            /// 获取或设置列的固定位置。
            /// </summary>
            /// <returns></returns>
            [Category("外观")]
            [Description("获取或设置列的固定位置。")]
            [DefaultValue(typeof(ColumnFixedStyle), "None")]
            public ColumnFixedStyle Fixed
            {
                get => (ColumnFixedStyle)this.GridColumn.Fixed;
                set => this.GridColumn.Fixed = (DevExpress.XtraGrid.Columns.FixedStyle)value;
            }

            /// <summary>
            /// 获取或设置列的宽度。
            /// </summary>
            /// <returns></returns>
            [DefaultValue(100)]
            [Category("外观")]
            [Description("获取或设置列的宽度。")]
            public int Width
            {
                get => this.m_Width;
                set
                {
                    this.m_Width = value;
                    this.GridColumn.Width = this.Width;
                }
            }

            private int m_Width = 100;

            /// <summary>
            /// 获取一个对象，该对象表示该 编辑器 中所包含项的集合。
            /// </summary>
            [Bindable(true)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", typeof(UITypeEditor))]
            public List<object> Items
            {
                get
                {
                    if (this.m_Items is null)
                        this.m_Items = new List<object>();
                    return this.m_Items;
                }
            }

            private List<object> m_Items;

            /// <summary>
            /// 获取或设置该列的下拉控件的实例。
            /// </summary>
            [DefaultValue(null)]
            [Category("编辑"), Description("获取或设置该列的下拉控件的实例。")]
            public PopupControl PopupControl { get; set; }

            /// <summary>
            /// 获取或设置该列的编辑器。
            /// </summary>
            [DefaultValue(typeof(ColumnEditEnum), "None")]
            [Category("编辑")]
            [Description("获取或设置该列的编辑器。")]
            public ColumnEditEnum ColumnEdit
            {
                get => this.m_ColumnEdit;
                set
                {
                    if (this.m_ColumnEdit != value)
                    {
                        this.m_ColumnEdit = value;
                        switch (this.ColumnEdit)
                        {
                            case ColumnEditEnum.None:
                                this.GridColumn.ColumnEdit = null;
                                break;
                            case ColumnEditEnum.Button:
                                this.GridColumn.ColumnEdit = this.Grid.buttonEdit1;
                                break;
                            case ColumnEditEnum.Color:
                                this.GridColumn.ColumnEdit = this.Grid.ColorEdit1;
                                break;
                            case ColumnEditEnum.ComboList:
                                this.GridColumn.ColumnEdit?.Dispose();
                                this.GridColumn.ColumnEdit = new RepositoryItemComboBox();
                                if (this.GridColumn.ColumnEdit is RepositoryItemComboBox box)
                                {
                                    box.Items.AddRange(this.Items.ToArray());
                                    box.TextEditStyle = (DevExpress.XtraEditors.Controls.TextEditStyles)(this.AllowDirectType ? TextEditStyles.Standard : TextEditStyles.DisableTextEditor);
                                }
                                break;
                        }
                    }
                }
            }

            private ColumnEditEnum m_ColumnEdit;

            /// <summary>
            /// 逻辑型，是否允许用户直接输入不在列表项目中的内容。
            /// </summary>
            /// <returns></returns>
            [DefaultValue(true)]
            [Category("外观")]
            [Description("是否允许用户直接输入不在列表项目中的内容。")]
            public bool AllowDirectType
            {
                get => this.m_AllowDirectType;
                set
                {
                    this.m_AllowDirectType = value;
                    if (this.GridColumn.ColumnEdit is RepositoryItemComboBox)
                    {
                        ((RepositoryItemComboBox)this.GridColumn.ColumnEdit).TextEditStyle = (DevExpress.XtraEditors.Controls.TextEditStyles)(value ? TextEditStyles.Standard : TextEditStyles.DisableTextEditor);
                    }
                }
            }

            private bool m_AllowDirectType = true;

            /// <summary>
            /// 获取或设置列在表中是否显示。
            /// </summary>
            /// <returns></returns>
            [DefaultValue(true)]
            [Category("外观")]
            [Description("获取或设置列在表中是否显示。")]
            public bool Visible
            {
                get => this.m_Visible;
                set
                {
                    this.m_Visible = value;
                    this.GridColumn.Visible = value;
                }
            }

            private bool m_Visible = true;
            private TextAlignEnum _textAlign = TextAlignEnum.Default;
            /// <summary>
            /// 设置列中数据对齐方式。
            /// </summary>
            /// <returns></returns>
            [DefaultValue(typeof(TextAlignEnum), "Default")]
            public TextAlignEnum TextAlign
            {
                get => this._textAlign;
                set
                {
                    this._textAlign = value;
                    this.GridColumn.AppearanceCell.Options.UseTextOptions = true;
                    switch (value)
                    {
                        case TextAlignEnum.Default:
                            this.GridColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Default;
                            break;
                        case TextAlignEnum.Left:
                            this.GridColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
                            break;
                        case TextAlignEnum.Center:
                            this.GridColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                            break;
                        case TextAlignEnum.Right:
                            this.GridColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
                            break;
                    }
                }
            }


            /// <summary>
            /// 设置数值列允许输入的最大小数位数。
            /// </summary>
            [DefaultValue(4)]
            public int Decimals
            {
                get => this._decimals;
                set
                {
                    this._decimals = value;
                    this.GridColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    this.GridColumn.DisplayFormat.FormatString = "0." + new string('#', value);
                    if (this.GridColumn.ColumnEdit is not null)
                    {
                        this.GridColumn.ColumnEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                        this.GridColumn.ColumnEdit.EditFormat.FormatString = "0." + new string('#', value);
                    }
                }
            }

            private string _formatString;

            /// <summary>
            /// 字符型，返回或设置列的显示格式。
            /// </summary>
            [DefaultValue(null)]
            [Category("外观")]
            [Description("字符型，返回或设置列的显示格式。")]
            public virtual string FormatString
            {
                get => this._formatString;
                set
                {
                    this._formatString = value;
                    if (this.DataType == typeof(string))
                        this.GridColumn.ColumnEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.None;
                    else if (this.DataType == typeof(DateTime))
                        this.GridColumn.ColumnEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    else if (this.DataType == typeof(int) || this.DataType == typeof(long) || this.DataType == typeof(float) || this.DataType == typeof(short) || this.DataType == typeof(decimal))
                        this.GridColumn.ColumnEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    this.GridColumn.DisplayFormat.FormatString = value;
                }
            }

            private int _decimals = 4;

            [Browsable(false)]
            public DevExpress.XtraGrid.Columns.GridColumn GridColumn
            {
                get => this._gridColumn;
            }
            internal DevExpress.XtraGrid.Columns.GridColumn _gridColumn;



            // Public Property GridBand As Object

            private void ParentBand(GridBand GridBand, bool Visible)
            {
                // GridBand.Visible = Visible
                // If GridBand.ParentBand IsNot Nothing Then
                // Dim V As Boolean = Visible
                // For Each Item As BandedGridColumn In GridBand.ParentBand.Columns
                // If Item.Visible = True Then
                // V = True
                // Exit For
                // End If
                // Next
                // If V = False Then
                // For Each Item As GridBand In GridBand.ParentBand.Children
                // If Item.Visible = True Then
                // V = True
                // Exit For
                // End If
                // Next
                // End If
                // Me.ParentBand(GridBand.ParentBand, V)
                // End If
            }

            /// <summary>
            /// 获取或设置列的名称。
            /// </summary>
            /// <returns></returns>
            [Category("数据编辑")]
            [Description("获取或设置列的名称。")]
            [Localizable(true)]
            [RefreshProperties(RefreshProperties.All)]
            [DefaultValue("")]
            public virtual string Name
            {
                get => this._name;
                set
                {
                    this._name = value;
                    this.GridColumn.Name = value;
                    this.GridColumn.Caption = this.Caption;
                }
            }

            private string _name;
            private string _fieldName;
            /// <summary>
            /// 获取或设置绑定的字段名称
            /// </summary>
            /// <returns></returns>
            [Category("数据编辑")]
            [Description("获取或设置绑定的字段名称。")]
            [Localizable(true)]
            [RefreshProperties(RefreshProperties.All)]
            public virtual string FieldName
            {
                get
                {
                    if (string.IsNullOrEmpty(this._fieldName))
                        return this.Name;
                    return this._fieldName;
                }
                set
                {
                    this._fieldName = value;
                    this.GridColumn.FieldName = value;
                }
            }

            /// <summary>
            /// 返回或设置列的计算表达式。
            /// 只有表达式列或者通过代码增加的临时列，才可以设置expression属性。
            /// </summary>
            [DefaultValue(null)]
            [Category("数据编辑")]
            [Description("返回或设置列的计算表达式。只有表达式列或者通过代码增加的临时列，才可以设置Expression属性。")]
            public virtual string Expression
            {
                get => this._expression;
                set
                {
                    this._expression = value;
                    this.GridColumn.UnboundExpression = this.Expression;
                }
            }
            private string _expression = null;

            /// <summary>
            /// 指定该列是否未绑定，如果是，则指定它存储的数据类型。
            /// </summary>
            [DefaultValue(UnboundColumnType.Bound)]
            public virtual UnboundColumnType UnboundType
            {
                get => this._unboundType;
                set
                {
                    this._unboundType = value;
                    this.GridColumn.UnboundType = (DevExpress.Data.UnboundColumnType)value;
                }
            }

            private UnboundColumnType _unboundType = UnboundColumnType.Bound;
        }
    }
}